package com.cloudlead.userAction.Service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.common.worker.security.PasswordHelper;
import com.cloudlead.commonData.service.BaseCompanyUserService;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseCompanyUser;
import com.cloudlead.plugin.security.service.ScUserHasPrivilegeService;
import com.cloudlead.plugin.security.service.ScUserService;
import com.cloudlead.plugin.security.vo.ScPrivilege;
import com.cloudlead.plugin.security.vo.ScUser;
import com.cloudlead.plugin.security.vo.ScUserHasPrivilege;
import com.cloudlead.userAction.Service.UserRegisterService;
import com.cloudlead.userService.UserManagement;
import com.cloudlead.userService.vo.UserManagement1VO;
import com.cloudlead.userService.vo.UserManagementVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015-08-13.
 */
@Service
public class UserRegisterImpl implements UserRegisterService {
    @Autowired
    BaseCompanyUserService baseCompanyUserService;

    @Autowired
    ScUserService scUserService;

    @Autowired
    ScUserHasPrivilegeService scUserHasPrivilegeService;
    @Override
    @Transactional//事务回滚
    public String userRegister(BaseCompanyUser baseCompanyUser) {
        baseCompanyUser.setFlag("0");
        baseCompanyUserService.create(baseCompanyUser);
        ScUser scUser=new ScUser();
        scUser.setLocked(false);
        scUser.setMobile(baseCompanyUser.getPhone());
        scUser.setEmail(baseCompanyUser.geteMail());
        scUser.setPassword(PasswordHelper.encryption(baseCompanyUser.getPassword(), null));
        scUser.setUsername(baseCompanyUser.getUserName());
        scUserService.create(scUser);

        return "成功";
    }

    @Override
    public void editUser(UserManagementVO vo) {
        BaseCompanyUser bcu = baseCompanyUserService.view(vo.getId());
        BeanUtils.copyProperties(vo, bcu);
        if(vo.getBaseCompanyId()!=null){//如果公司不为空
            BaseCompany bcy = new BaseCompany();
            bcy.setId(vo.getBaseCompanyId());
            bcu.setBaseCompany(bcy);
        }
        bcu.setUserName(vo.getUserName());
        bcu.setFlag("0");
        bcu.setName(vo.getName());
        bcu.seteMail(vo.geteMail());
        bcu.setPassword(vo.getPassword());
        bcu.setStatus(vo.getStatus());
        bcu.setPermission(vo.getPermission());
        bcu.setPhone(vo.getPhone());
        bcu.setWeixinNamee(vo.getWeixinNamee());
        bcu.setUserID(vo.getUserID());
        baseCompanyUserService.update(bcu);
        ScUser scQry=new ScUser();
        scQry.setUsername(vo.getUserName());
        ScUser scuser = scUserService.findOne(scQry);
        if (scuser!=null) {
            //BeanUtils.copyProperties(vo, scuser);
            scuser.setUsername(vo.getUserName());
            scuser.setEmail(vo.geteMail());
            scuser.setPassword(PasswordHelper.encryption(vo.getPassword(), null));
            scuser.setMobile(vo.getPhone());
            scUserService.update(scuser);
        }
    }

    @Override
    //单值查询
    public Object findOneUser(UserManagement1VO userManagement1VO) {
        BaseCompanyUser bcu=new BaseCompanyUser();
        BeanUtils.copyProperties(userManagement1VO, bcu);
        BaseCompanyUser bcu1=baseCompanyUserService.findOne(bcu);
        System.out.println(bcu1.getName()+"==================");
        return bcu1;
    }

    @Override
    //分页查询
    public Page<UserManagement1VO> finAllUser(UserManagement1VO userManagement1VO, int size, int page) {
        PageRequest pr=new PageRequest();
        List<Term> tr=new ArrayList<>();
        tr.add(new Term("flag","0"));//未删除的
        if(userManagement1VO!=null){
            if (userManagement1VO.getBaseCompanyId()!=null && !userManagement1VO.getBaseCompanyId().equals(""))
                tr.add(new Term("baseCompanyId",userManagement1VO.getBaseCompanyId()));
            if (userManagement1VO.getUserName()!=null && !userManagement1VO.getUserName().equals(""))
                tr.add(new Term("userName",userManagement1VO.getUserName()));
            if (userManagement1VO.getName()!=null && !userManagement1VO.getName().equals(""))
                tr.add(new Term("name",userManagement1VO.getName()));
            if (userManagement1VO.getPhone()!=null && !userManagement1VO.getPhone().equals(""))
                tr.add(new Term("phone",userManagement1VO.getPhone()));
        }
        Filter filter=new Filter(new Criteria(tr));
            pr.setSize(size);//每页的行数
            pr.setPage(page);//页码
            pr.setFilter(filter);
        Page<BaseCompanyUser> bsu=baseCompanyUserService.findAll(pr);
        Page<UserManagement1VO>  um=new Page<>();
        BeanUtils.copyProperties(bsu,um);
        return um;
    }

    @Override
    @Transactional
    public void delUser(String id) {
        BaseCompanyUser bcu=baseCompanyUserService.view(id);
        List<Term> tr = new ArrayList<Term>();
        tr.add(new Term("username",bcu.getUserName()));
        scUserService.deleteAll(new Filter(new Criteria(tr)));
        baseCompanyUserService.delete(id);
//        bcu.setFlag("1");
//        baseCompanyUserService.update(bcu);
    }

    @Override
    public String addPrivilege(String userName, String privilegeList) {
        //获取用户ID
        ScUser scUserss = new ScUser();
        scUserss.setUsername(userName);
        String userId = scUserService.findOne(scUserss).getId();
        //删除权限
        ScUserHasPrivilege scUserHasPrivileges = new ScUserHasPrivilege();
        ScUser scUsers = new ScUser();
        scUsers.setId(userId);
        scUserHasPrivileges.setScUser(scUsers);
        scUserHasPrivilegeService.deleteAll(scUserHasPrivileges);
        //更新权限
        if(!privilegeList.equals("")){
        String [] priList =privilegeList.split(",");
        for(int i =0;i<priList.length;i++){
            ScUserHasPrivilege scUserHasPrivilege = new ScUserHasPrivilege();
            ScUser scUser = new ScUser();
            scUser.setId(userId);
            scUserHasPrivilege.setScUser(scUser);
            ScPrivilege scPrivilege = new ScPrivilege();
            scPrivilege.setId(priList[i]);
            scUserHasPrivilege.setScPrivilege(scPrivilege);
            scUserHasPrivilege.setIsPermission(true);
            if(scUserHasPrivilegeService.findOne(scUserHasPrivilege) == null){
                scUserHasPrivilegeService.create(scUserHasPrivilege);
            }
          }
        }
        return "成功";
    }

    @Override
    public String delPrivilege(String userId) {
        ScUserHasPrivilege scUserHasPrivilege = new ScUserHasPrivilege();
        ScUser scUser = new ScUser();
        scUser.setId(userId);
        scUserHasPrivilege.setScUser(scUser);
        scUserHasPrivilegeService.deleteAll(scUserHasPrivilege);
        return "成功";
    }

    @Override
    public void editPass(String userID, String password, String editPass) {
        BaseCompanyUser bcu=baseCompanyUserService.view(userID);
        if(!bcu.getPassword().equals(password)){
            throw new RestClientException("旧密码不正确。");}
        bcu.setPassword(editPass);
        bcu.setFlag("0");
        baseCompanyUserService.update(bcu);
        ScUser scQry=new ScUser();
        scQry.setUsername(bcu.getUserName());
        ScUser scuser = scUserService.findOne(scQry);
        if (scuser!=null) {
            scuser.setPassword(PasswordHelper.encryption(editPass, null));
            scUserService.update(scuser);
        }
    }

}
